package com.gistone.commis.api.restController;

import com.gistone.commis.dto.EditorResult;
import com.gistone.commis.dto.FieldError;
import com.gistone.commis.dto.Option;
import com.gistone.commis.dto.Options;
import com.gistone.commis.entity.*;
import com.gistone.commis.mapper.CommunityMapper;
import com.gistone.commis.mapper.DepartMapper;
import com.gistone.commis.mapper.HouseLogMapper;
import com.gistone.commis.util.DataTablePageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wangfan on 2017/5/5.
 */
@RestController("apiHouseLogController")
@RequestMapping("api/houseLog")
public class HouseLogController {

    private static final Logger logger = LoggerFactory.getLogger(HouseLogController.class);


    @Autowired
    private HouseLogMapper houseLogMapper;

    @Autowired
    private CommunityMapper communityMapper;

    @Autowired
    private DepartMapper departMapper;









    /**
     * dataTables 表单数据返回
     *
     * @param request
     * @return
     */

    @Transactional
    @RequestMapping(method = RequestMethod.GET, value = "/pageDatatables")
    public DataTablePageUtil<HouseLog> pageDatatables(HttpServletRequest request, HouseLog houseLog) {
/*        HttpSession httpSession = request.getSession();
        User user = (User) httpSession.getAttribute("userInfo");
        logger.info("用户信息：" + user.toString());
        int community_id = user.getCommunity().getCommunityId();
        houseLog.setCommunityId(community_id);*/

        List<Community> communities =communityMapper.findCommunitys();
        Options os =new Options();
        List<Option> optionList =new ArrayList<>();
        for (Community community : communities) {

            Option option =new Option();
            option.setLabel(community.getCommunityName());
            option.setValue(community.getCommunityId());
            optionList.add(option);


        }
        os.setCommunityId(optionList);
        List<Depart> departs =departMapper.findDeparts();
        optionList =new ArrayList<>();
        for (Depart depart : departs) {

            Option option =new Option();
            option.setLabel(depart.getDepartName());
            option.setValue(depart.getDepartId());
            optionList.add(option);


        }

        os.setDepartId(optionList);




        DataTablePageUtil<HouseLog> dataTable = new DataTablePageUtil<>(request);



        PageHelper.startPage(dataTable.getPage_num(), dataTable.getPage_size());
        List<HouseLog> list = houseLogMapper.selectSelective(houseLog);
        PageInfo<HouseLog> pageInfo =new PageInfo<>(list);
        System.out.println(list.size());
        dataTable.setDraw(dataTable.getDraw());
        dataTable.setData(pageInfo.getList());
        dataTable.setOptions(os);
        dataTable.setRecordsTotal((int)pageInfo.getTotal());
        dataTable.setRecordsFiltered(dataTable.getRecordsTotal());
        //返回数据到页面


        return dataTable;

    }


}
